<?php

include_once 'util/arrayutil.php';
include_once 'genre.php';

class Movie {

    private $id;
    private $title;
    private $director;
    private $released;
    private $filmedIn;
    private $badMovie;
    private $genre;
    private $description;

    

    function setFilmedIn($filmedIn) {
        $this->filmedIn = $filmedIn;
    }

    function getFilmedIn() {
        return $this->filmedIn;
    }

    function setBadMovie($badmovie) {
        $this->badMovie = $badmovie;
    }

    function getBadMovie() {
        return $this->badMovie;
    }

    function setDescription($description) {
        $this->description = $description;
    }

    function getDescription() {
        return $this->description;
    }

    function setId($id) {
        $this->id = $id;
    }

    function getId() {
        return $this->id;
    }

    function setDirector($director) {
        $this->director = $director;
    }

    function getDirector() {
        return $this->director;
    }

    function setReleased($released) {
        $this->released = $released;
    }

    function setGenre($_genre) {
        if ($_genre instanceof Genre) {
            $this->genre = $_genre;
        }
    }

    function getGenre() {
        return $this->genre;
    }

    public function setTitle($_title) {
        $this->title = $_title;
    }

    public function getTitle() {
        return $this->title;
    }

    public function save() {
        $genre_id = $this->genre->getId();
        mysql_query("insert into movie(title,director,released,filmed_id,bad_movie,genre,
          descriptoin) values('$this->title','$this->director','$this->released',
          '$this->filmedIn','$this->badMovie',$genre_id,'$this->description')");
    }

    public static function load($id) {
        $result = mysql_query("select id,title,director,released,filmed_id,bad_movie,
            genre,description from movie where id=$id") or die("movie: " . mysql_error());
        $row = mysql_fetch_row($result);
        $movie = new Movie();
        $movie->setId($row[0]);
        $movie->setTitle($row[1]);
        $movie->setDirector($row[2]);
        $movie->setReleased($row[3]);
        $movie->setFilmedIn($row[4]);
        $movie->setBadMovie($row[5]);
        $movie->setGenre(Genre::load($row[6]));        
        $movie->setDescription($row[7]);
        return $movie;
    }

    public static function loads() {
        $result = mysql_query("select id,title,director,released,filmed_id,bad_movie,
            genre,description from movie");
        $movies = array();
        while ($row = mysql_fetch_array($result)) {
            $movie = new Movie();
            $movie->setId($row[0]);
            $movie->setTitle($row[1]);
            $movie->setDirector($row[2]);
            $movie->setReleased($row[3]);
            $movie->setFilmedIn($row[4]);
            $movie->setBadMovie($row[5]);
            $movie->setGenre(Genre::load($row[6]));
            $movie->setDescription($row[7]);
            array_push($movies, $movie);
        }
        return $movies;
    }
    
      public static function loadsPaging($start,$end) {     
        $result = mysql_query("select id,title,director,released,filmed_id,bad_movie,
            genre,description from movie limit $start,$end");
        $movies = array();
        while ($row = mysql_fetch_array($result)) {
            $movie = new Movie();
            $movie->setId($row[0]);
            $movie->setTitle($row[1]);
            $movie->setDirector($row[2]);
            $movie->setReleased($row[3]);
            $movie->setFilmedIn($row[4]);
            $movie->setBadMovie($row[5]);
            $movie->setGenre(Genre::load($row[6]));
            $movie->setDescription($row[7]);
            array_push($movies, $movie);
        }
        return $movies;
    }

    public static function loadToJSON($id) {
        return json_encode(ArrayUtil::arrayOfObjectToArray(Movie::load($id)));
    }
    
    public static function loadsToJSON() {
        return json_encode(ArrayUtil::arrayOfObjectToArray(Movie::loads()));
    }
    
    public static function loadsPagingToJSON($start,$end){
        return json_encode(ArrayUtil::arrayOfObjectToArray(Movie::loadsPaging($start, $end)));
    }
    
    

}

?>
